package com.diamond.action;


import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.diamond.bo.DiamondBO;
import com.diamond.bo.ReturnBO;
import com.diamond.entity.Diamond;
import com.diamond.facade.CommonFacade;

@SuppressWarnings("unchecked")
public class Page{

	static final public int PAGESIZE = 9;
	
	
	public static List util(HttpServletRequest request,String url,CommonFacade commonFacade,String hql){
		return util(request,url,commonFacade,hql,PAGESIZE);
	}
	
	/**
	 * 多条件分页公共解决方案
	 * @param request
	 * @param service
	 * @param dc
	 * @param reultName
	 * @param pagename
	 * @param param
	 * @param pagesize
	 * @throws Exception
	 */
	public static List util(HttpServletRequest request,String url,CommonFacade commonFacade,String hql,int pagesize){
		try {
		request.setAttribute("url", url);	
		String currpage = request.getParameter("currpage");//获得当前页
		if (currpage == null) {currpage = "1";}
		Integer rowcount=0;
		List list=null;
		Map map = commonFacade.findPage(Integer.parseInt(currpage), pagesize, hql);
		if(map!=null){
			rowcount=(Integer) map.get("total");
			list = (List) map.get("list");
		}
		request.setAttribute("currpage", currpage);
		request.setAttribute("rowcount", rowcount);
		request.setAttribute("totalpage", rowcount % pagesize > 0 ? rowcount / pagesize + 1: rowcount / pagesize);
		return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 前台多条件分页公共解决方案
	 * @param request
	 * @param service
	 * @param dc
	 * @param reultName
	 * @param pagename
	 * @param param
	 * @param pagesize
	 * @throws Exception
	 */
	public static List stageUtil(Class cls,HttpServletRequest request,String url,CommonFacade commonFacade,String sql,int pagesize){
		try {
		request.setAttribute("url", url);	
		String currpage = request.getParameter("currpage");//获得当前页
		if (currpage == null) {currpage = "1";}
		Integer rowcount=0;
		List list=null;
		Map map = commonFacade.findCountAndPageBySql(Integer.parseInt(currpage), pagesize,sql,cls);
		if(map!=null){
			rowcount=(Integer) map.get("total");
			list = (List) map.get("list");
		}
		request.setAttribute("currpage", currpage);
		request.setAttribute("rowcount", rowcount);
		request.setAttribute("totalpage", rowcount % pagesize > 0 ? rowcount / pagesize + 1: rowcount / pagesize);
		return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 后台分页解决方案
	 * @param request
	 * @param service
	 * @param dc
	 * @param reultName
	 * @param pagename
	 * @param param
	 * @param pagesize
	 * @throws Exception
	 */
	public static ReturnBO<Diamond> backUtil(HttpServletRequest request,String url,CommonFacade commonFacade,String hql,int start, int pagesize){
		ReturnBO<Diamond> returnBo = new ReturnBO<Diamond>();
		try {
		request.setAttribute("url", url);	
		String currpage = String.valueOf(start/pagesize + 1);
		if (currpage == null) {currpage = "1";}
		Integer rowcount=0;
		List list=null;
		Map map = commonFacade.findPage(Integer.parseInt(currpage), pagesize, hql);
		if(map!=null){
			rowcount=(Integer) map.get("total");
			list = (List) map.get("list");
		}
		returnBo.setTotalSize(rowcount);
		returnBo.setPageSize(pagesize);
		returnBo.setStart(start);
		returnBo.setReturnBolist(list);
		return returnBo;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
}
